package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.*;

public class AdminCategoryServiceImpl implements AdminCategoryService {
    public List<HashMap<String, String>> l1(int limit, int page) {
        //1、数据库session
        SqlSession session = MybatisUtils.getSession();
        //2、mapper
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);


        PageHelper.startPage(page,limit);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        List<HashMap<String, String>> list=new ArrayList<>();

        for (MarketCategory marketCategory : marketCategories) {
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("value", marketCategory.getId().toString());
            stringStringHashMap.put("label", marketCategory.getName());
            list.add(stringStringHashMap);
        }

        session.close();
        return list;
        //3、查询
        //4、返回

        //5、关闭连接


    }

    @Override
    public String list() {
        //1、数据库session
        SqlSession session = MybatisUtils.getSession();
        //2、mapper
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);


        HashMap<String, List<MarketCategory>> pIdListHashMap = new HashMap<>();

        for (MarketCategory marketCategory : marketCategories) {
//            List<MarketCategory> pIdList=pIdListHashMap.getOrDefault(marketCategory.getPid(),new ArrayList<MarketCategory>());
//
//            pIdList.add(marketCategory);
//            pIdListHashMap.put(String.valueOf(marketCategory.getPid()),pIdList);
            List<MarketCategory> pIdList = pIdListHashMap.get(String.valueOf(marketCategory.getPid()));
            if (pIdList == null) {
                pIdList = new ArrayList<>();
                pIdListHashMap.put(String.valueOf(marketCategory.getPid()), pIdList);
            }
            pIdList.add(marketCategory);
        }
        session.close();

        //构建想要的数据结构类型
        //3、查询
        //4、返回

        //5、关闭连接
        //将信息转为Json字符串
        try {
            List<HashMap<String, Object>> list = null;

            list = listMap(pIdListHashMap);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            if (list != null && (!list.isEmpty())) {
                Object okList = ResponseUtil.okList(list);
                String respJson = null;
                respJson = objectMapper.writeValueAsString(okList);
                return respJson;
            }

            Object fail = ResponseUtil.fail(700, "查询失败");
            String failJson = objectMapper.writeValueAsString(fail);
            return failJson;
            }catch(Exception e){
                throw new RuntimeException(e);
            }

    }

    @Override
    public int delete(Integer id) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        int effectrows=mapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return effectrows;

    }

    @Override
    public int create(MarketCategory marketCategory) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setUpdateTime(LocalDateTime.now());
        int effectRows=mapper.insertSelective(marketCategory);
        session.commit();
        session.close();
        return  effectRows;
    }

    @Override
    public int update(MarketCategory marketCategory) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        marketCategory.setUpdateTime(LocalDateTime.now());
        int effectRows=marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
        session.commit();
        session.close();
        return effectRows;
    }

    private List<HashMap<String, Object>> listMap(Map<String,List<MarketCategory>> map) throws JsonProcessingException {
        List<HashMap<String, Object>> list=new ArrayList<>();
        List<MarketCategory> marketCategories = map.get("0");

        for (MarketCategory marketCategoryBig : marketCategories) {

            HashMap<String, Object> parentMarketCategoryMap = parentMarketCategoryVO(marketCategoryBig, map);
            if(parentMarketCategoryMap==null){
                continue;
            }
            list.add(parentMarketCategoryMap);
        }
        return list;
    }


    //HashMap()
    private HashMap<String, Object> parentMarketCategoryVO(MarketCategory marketCategoryBig, Map<String, List<MarketCategory>> map) throws JsonProcessingException {


        HashMap<String, Object> parentHashMap = new HashMap<>();
        parentHashMap.put("desc", String.valueOf(marketCategoryBig.getDesc()));
        parentHashMap.put("iconUrl", String.valueOf(marketCategoryBig.getIconUrl()));
        parentHashMap.put("id", String.valueOf(marketCategoryBig.getId()));
        parentHashMap.put("keywords", String.valueOf(marketCategoryBig.getKeywords()));
        parentHashMap.put("level", String.valueOf(marketCategoryBig.getLevel()));
        parentHashMap.put("name", String.valueOf(marketCategoryBig.getName()));
        parentHashMap.put("picUrl", String.valueOf(marketCategoryBig.getPicUrl()));

        //children
        List<Map<String,String>> chrildMapLists = new ArrayList<>();

        parentHashMap.put("children",chrildMapLists);


        List<MarketCategory> marketCategories = map.get(String.valueOf(marketCategoryBig.getId()));

        if(marketCategories==null || marketCategories.isEmpty()){
            return parentHashMap;
        }




        for (MarketCategory marketCategorySmall :marketCategories) {
            HashMap<String, String> chrildMap = new HashMap();
            chrildMap.put("id",marketCategorySmall.getId().toString());
            chrildMap.put("desc",marketCategorySmall.getDesc());
            chrildMap.put("iconUrl",marketCategorySmall.getIconUrl());
            chrildMap.put("keywords",marketCategorySmall.getKeywords());
            chrildMap.put("level",marketCategorySmall.getLevel());
            chrildMap.put("name",marketCategorySmall.getName());
            chrildMap.put("picUrl",marketCategorySmall.getPicUrl());
            chrildMapLists.add(chrildMap);
        }


        return parentHashMap;
    }
}
